package com.vvv.shortlink.project.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vvv.shortlink.common.convention.excepton.ClientException;
import com.vvv.shortlink.project.dao.entity.LinkDO;
import com.vvv.shortlink.project.dao.mapper.LinkMapper;
import com.vvv.shortlink.project.dto.req.RecycleBinPageReqDTO;
import com.vvv.shortlink.project.dto.req.RecycleBinRecoverReqDTO;
import com.vvv.shortlink.project.dto.req.RecycleBinRemoveReqDTO;
import com.vvv.shortlink.project.dto.req.RecycleBinSaveReqDTO;
import com.vvv.shortlink.project.dto.resp.LinkPageRespDTO;
import com.vvv.shortlink.project.service.RecycleBinService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import static com.vvv.shortlink.project.constant.RedisKeyConstant.SHORT_LINK_GOTO_IS_NULL_KEY;
import static com.vvv.shortlink.project.constant.RedisKeyConstant.SHORT_LINK_GOTO_ORIGIN_KEY;

/**
 * @author vvv
 * @date 2024/2/19 14 55
 * discription
 */
@Service
@RequiredArgsConstructor
public class RecycleBinServiceImpl extends ServiceImpl<LinkMapper, LinkDO> implements RecycleBinService {

    private final StringRedisTemplate stringRedisTemplate;

    @Override
    public void saveLinkToBin(RecycleBinSaveReqDTO requestParam) {
        String fullShortUrl = requestParam.getFullShortUrl();
        String gid = requestParam.getGid();
        Long userId = requestParam.getUserId();
        if (StringUtils.isAnyEmpty(fullShortUrl, gid) || userId == null) {
            throw new ClientException("参数不能为空");
        }
        LambdaUpdateWrapper<LinkDO> updateWrapper = Wrappers.lambdaUpdate(LinkDO.class)
                .eq(LinkDO::getGid, gid)
                .eq(LinkDO::getUserId, userId)
                .eq(LinkDO::getFullShortUrl, fullShortUrl)
                .eq(LinkDO::getEnableStatus, 0);
        LinkDO linkDO = LinkDO.builder()
                .enableStatus(1)
                .build();
        baseMapper.update(linkDO, updateWrapper);
        // 把该短链在redis缓存中删掉
        String shortLinkCacheKey = SHORT_LINK_GOTO_ORIGIN_KEY + fullShortUrl;
        stringRedisTemplate.delete(shortLinkCacheKey);
    }

    @Override
    public IPage<LinkPageRespDTO> pageShortLinkInBin(RecycleBinPageReqDTO requestParam) {
        Long userId = requestParam.getUserId();
        LambdaQueryWrapper<LinkDO> queryWrapper = Wrappers.lambdaQuery(LinkDO.class)
                .eq(LinkDO::getUserId, userId)
                .eq(LinkDO::getEnableStatus, 1)
                .orderByDesc(LinkDO::getCreateTime);
        IPage<LinkDO> linkDOIPage = baseMapper.selectPage(requestParam, queryWrapper);
        return linkDOIPage.convert(linkDO -> {
            LinkPageRespDTO result = BeanUtil.toBean(linkDO, LinkPageRespDTO.class);
            result.setDomain("http://" + result.getDomain());
            return result;
        });
    }

    @Override
    public void recoverShortLink(RecycleBinRecoverReqDTO requestParam) {
        String fullShortUrl = requestParam.getFullShortUrl();
        String gid = requestParam.getGid();
        Long userId = requestParam.getUserId();
        if (StringUtils.isAnyEmpty(fullShortUrl, gid) || userId == null) {
            throw new ClientException("参数不能为空");
        }
        LambdaUpdateWrapper<LinkDO> updateWrapper = Wrappers.lambdaUpdate(LinkDO.class)
                .eq(LinkDO::getGid, gid)
                .eq(LinkDO::getUserId, userId)
                .eq(LinkDO::getFullShortUrl, fullShortUrl)
                .eq(LinkDO::getEnableStatus, 1);
        LinkDO linkDO = LinkDO.builder()
                .enableStatus(0)
                .build();
        baseMapper.update(linkDO, updateWrapper);
        // 把该短链在redis空值缓存中删掉
        String shortLinkIsNullKey = SHORT_LINK_GOTO_IS_NULL_KEY + fullShortUrl;
        stringRedisTemplate.delete(shortLinkIsNullKey);
    }

    @Override
    public void removeShortLinkFromBin(RecycleBinRemoveReqDTO requestParam) {
        String fullShortUrl = requestParam.getFullShortUrl();
        String gid = requestParam.getGid();
        Long userId = requestParam.getUserId();
        if (StringUtils.isAnyEmpty(fullShortUrl, gid) || userId == null) {
            throw new ClientException("参数不能为空");
        }
        LambdaUpdateWrapper<LinkDO> updateWrapper = Wrappers.lambdaUpdate(LinkDO.class)
                .eq(LinkDO::getGid, gid)
                .eq(LinkDO::getUserId, userId)
                .eq(LinkDO::getFullShortUrl, fullShortUrl)
                .eq(LinkDO::getEnableStatus, 1);
        baseMapper.delete(updateWrapper);
    }
}
